/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
/*
HC,
20250328,
history:
version1:
struct queOptStruct
{
    uint32_t ridx;// read index
    uint32_t widx;// write index
    uint32_t datalen;// data len
    uint32_t bfSize;// buffer total size
    uint8_t *bfSave;// buffer save ptr
};
version2:
struct queOptStruct
{
    uint32_t ridx;// read index
    uint32_t widx;// write index
    uint32_t bfSize;// buffer total size
    uint8_t *bfSave;// buffer save ptr
};
version3:
struct queOptStruct
{
    uint32_t ridx;// read index
    uint32_t widx;// write index
    uint32_t bfSize;// buffer total size
    uint8_t bfSave[1];// buffer save ptr
};
version4:
struct queOptStruct
{
    uint32_t ridx[4];// read index
    uint32_t widx;// write index
    uint32_t bfSize;// buffer total size
    uint8_t bfSave[1];// buffer save ptr
};
version5:
struct queOptStruct
{
    uint8_t *bfSave;// buffer save ptr
    uint32_t widx;// write index
    uint32_t bfSize;// buffer total size
    uint32_t readerNumber;
    uint32_t ridx[1];// read index
};
*/
#ifndef __QUEUE_BASE_OPERATE_H__
#define __QUEUE_BASE_OPERATE_H__
#if 0
#include <typecomm.h>
#define QUE_WITH_USR_INF 0
typedef uint32_t queOptType;
#define QUE_OPT_READ_SIZE 1
struct queOptStruct
{
#if QUE_WITH_USR_INF
    char usrInf[8];
#endif
    queOptType ridx[QUE_OPT_READ_SIZE];// read index
    queOptType widx;// write index
    queOptType bfSize;// buffer total size
    uint8_t bfSave[1];// buffer save ptr
};

int queOptInit(void *qptr, queOptType optSize);
queOptType queOptWrite(void *qptr, const uint8_t *iobuf, queOptType len);
queOptType queOptSize(void *qptr, uint8_t rdChnl);
queOptType queOptRead(void *qptr, uint8_t rdChnl, uint8_t *iobuf, queOptType len);
queOptType queOptRdFix(void *qptr, uint8_t rdChnl, uint8_t *iobuf, queOptType len);

#define queOptHeadSize (offsetof(struct queOptStruct, bfSave))
#endif
#include <typecomm.h>
#if DEVICE_PLATFORM_CONFIGURE != DEVICE_PLATFORM_C51_MCU
// g++ 4.3.6, class define buffer[], use these function, data is error. use pack(1) ok.
#pragma pack(1)
#endif
typedef uint32_t queOptType;
typedef struct
{
    // uint8_t *bfSave;    // que buffer save ptr. shm need not ptr!
    queOptType bfSize;  // buffer total size
    queOptType widx;    // write index
    queOptType rdrNbr;  // reader number.
    queOptType ridx[1]; // reader id
} queOptStruct;

int queOptInit(void *qptr, queOptType optSize, queOptType cfgRdrNbr);
queOptType queOptWrite(void *qptr, const uint8_t *iobuf, queOptType len);
queOptType queOptSize(void *qptr, uint8_t rdChnl);
queOptType queOptRead(void *qptr, uint8_t rdChnl, uint8_t *iobuf, queOptType len);
queOptType queOptResetR(void *qptr, uint8_t rdChnl);
queOptType queOptRdFix(void *qptr, uint8_t rdChnl, uint8_t *iobuf, queOptType len);

#define queGetHeadSize(rdrNbr) ((offsetof(queOptStruct, ridx)) + sizeof(queOptType) * rdrNbr)
#define calcQueTotalSize(rdrNbr, saveLen) ((offsetof(queOptStruct, ridx)) + sizeof(queOptType) * rdrNbr + saveLen)
#define queGetDataPtr(qptr) (((uint8_t *)qptr) + queGetHeadSize(((queOptStruct *)qptr)->rdrNbr))
#if DEVICE_PLATFORM_CONFIGURE != DEVICE_PLATFORM_C51_MCU
#pragma pack()
#endif
#endif
